<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>魏远商城</title>
		<link rel="stylesheet" type="text/css" href="../../css/common/public.css"/>
		<link rel="stylesheet" type="text/css" href="../../css/cart/logistics.css" />
	</head>

	<body>
		<!--顶部代码-->
		<div class="top_all">
		<div class="top">
			<div class="top_con">
				<div class="tlogin"></div>
				<div class="gotoregister" style="display: none;">免费注册</div>
				<div class="homepage">商城首页</div>
				<div class="tmyorder">我的订单<img src="../../image/turn_down.png"></div>						
				<div class="home_car"><img src="../../image/shop_car.png">购物车<span class="car_time">0</span></div>
				<div class="line_ke">联系客服<img src="../../image/turn_down.png"></div>							
			</div>		
		</div>
		<div class="loged" style="display: none;">
			<div><span class="guanli">账号管理</span>|<span class="out">退出</span></div>
			<div class="thead">
				<div class="thead_pic"><span></span></div>
				<div class="tmenber">企业会员</div>
			</div>
		</div>
		<div class="myorder">
			<span class="order_1">待支付</span>
			<span class="order_2">待发货</span>
			<span class="order_3">待收货</span>
			<span class="order_4">待评价</span>
		</div>
		<div class="tcont">
			<span class="mine1">用户帮助</span>
			<span class="mine2">联系客服</span>
			<span class="mine3">投诉建议</span>
			<span class="mine4">退换货</span>
			<span class="mine5">收货地址管理</span>
		</div>
		
		<div class="logeandsear">
			<div class="imageLogo"><img src="../../image/Logo.png"></div>
			<div class="search">
				<div>
					<img class="search-icon" src="../../image/search-icon.png"><input type="text" class="home_searchto" placeholder="魏远产品搜索" /><span class="gotosearch">搜索</span><span class="searchtips">X请输入您要搜索的内容</span>
				</div>
			</div>
	    </div>
	</div>	
	<div class="right">
			<div class="right_car"><span><img src="../../image/righr_car.png"></br>购</br>物</br>车</span></div>
		<div class="youhuiquan"><span>优</br>惠</br>券</span></div>
		<div class="qianbao"><span>钱</br>包</span></div>
		<div class="qiyeshangc"><span>企</br>业</br>商</br>城</span></div>
		<div class="totop"><span><br/>^<br/>TOP</span></div>
	</div>
	<!--<div class="top_all">
		<div class="top">
			<div class="top_con">
				<div class="tlogin">登录</div>
				<div>免费注册</div>
				<div class="tmyorder">我的订单<img src="../../image/turn_down.png"></div>						
				<div><img src="../../image/shop_car.png">购物车<span class="car_time">0</span><img src="../../image/turn_down.png"></div>
				<div class="line_ke">联系客服<img src="../../image/turn_down.png"></div>							
			</div>		
		</div>
		<div class="loged" style="display: none;">
			<div><span class="guanli">账号管理</span>|<span class="out">退出</span></div>
			<div class="thead">
				<div class="thead_pic"><span></span></div>
				<div class="tmenber">企业会员</div>
			</div>
		</div>
		<div class="myorder">
			<span>待支付</span>
			<span>待发货</span>
			<span>待收货</span>
			<span>待评价</span>
		</div>
		<div class="tcont">
			<span>用户帮助</span>
			<span>联系客服</span>
			<span>投诉建议</span>
			<span>退换货</span>
			<span>收货地址管理</span>
		</div>
		
		<div class="logeandsear">
			<div><img src="../../image/Logo.png"></div>
			<div class="search">
				<div>
					<img class="search-icon" src="../../image/search-icon.png"><input type="text" placeholder="魏远产品搜索" /><span class="gotosearch">搜索</span>					
				</div>
			</div>
	    </div>
	</div>	
	
	<!--右侧代码-->
	<!--<div class="right">
			<div class="right_car"><img src="../../image/righr_car.png"></br>购</br>物</br>车</div>
			<div>优</br>惠</br>券</div>
			<div>钱</br>包</div>
			<div>企</br>业</br>商</br>城</div>
			<div class="totop">——<br/>^<br/>TOP</div>
	</div>-->
	
	<div class="wrap">
		
	</div>
		<div class="main-log">
			<div>
				<!--配送-->
				<span><span>收货人信息</span><span class="main-add"><img src="../../image/logistics-1.png"/>管理收货地址</span></span>
				<!--未配送-->
				<!--<span>收货人信息</span>-->
				<span class="main-per">
					<!--配送-->
					<span class="main-per1">
						<span>
							<span class="name"></span>
							<span class="tel"></span>
						</span>
						<span>
							<span class="area"></span>
							<span class="details"></span>
						</span>
					</span>
					<!--<span class="main-per1">
						<span>
							<span class="name">anguler</span>
							<span class="tel">18316634979</span>
						</span>
						<span>
							<span class="area">广东省广州市天河区</span>
							<span class="details">车陂街道红利商务中心337号</span>
						</span>
					</span>-->
					<!--未配送-->
					<span class="main-per2"><img src="../../image/logistics.png"/></span>
				</span>
			</div>
			<div>
				<span>商品信息</span>
				<span class="main-good">
					<div class="main-goods2">
						<span>商品名称</span>
						<span>类型</span>
						<span>单价（元）</span>
						<span>数量</span>
						<span>总价</span>
					</div>
					<!--<div class="main-goods">
						<div style="background-image: url(../../img/cart-yp.png);"></div>
						<div class="main-goods1">
							<div></div>
							<div>
								<span>
									<span>6支组合 包邮 美国进口汽水饮料</span>
									<span>Coca可口可乐组合6X355ml</span>
								</span>
								<span>罐装</span>
								<span>￥36.00</span>
								<span>3</span>
								<span>￥110.40</span>
							</div>
						</div>
					</div>-->
				</span>
			</div>
			<div>
				<span>配送方式选择</span>
				<span class="main-ps">
					<input type="text" readonly="readonly" name="select" id="" value="" placeholder="请选择" />
					<img src="../../image/logistics.png-2.png"/>
				</span>
				<span class="main-ps1">
					<!--<input readonly="readonly" type="text" name="" id="" value="顺丰快递12" placeholder="请选择" />-->
				</span>
			</div>
			<div>
				<span>优惠卷</span>
				<span class="main-yhj main-ps">
					<input type="text" name="coupon" id="" value="" placeholder="请输入优惠卷号码" />
					<span class="coupon">使用</span>
				</span>
			</div>
			<div>
				<span>备注</span>
				<span class="main-bz">
					<textarea name="" rows="" cols="" placeholder="请输入您想备注的内容"></textarea>
				</span>
			</div>
			<div class="main-js">
				<span style="font-size: 16px;color: #666;"><span>商品总金额：</span><span class="moneyAll">￥0.00</span></span>
				<span><span>优惠：</span><span class="usecoun">￥0.00</span></span>
				<span><span>运费：</span><span class="useyun">￥0.00</span></span>
				<span><span>应付金额：</span><span class="newPrice">￥0.00</span></span>
			</div>
			<div class="main-dd">
				提交订单
			</div>
			<div class="main-error">收货人信息有误!</div>
		</div>
		<!--弹框-->
		<div class="pop1"></div>
		<div class="pop">
			<div class="pop-t">提示 <img class="pop-img" src="../../image/creat_cancel.png"/></div>
			<div>
				<div class="pop-text">抱歉！您还未登录！</div>
				<div class="pop-y">确认</div>
			</div>
		</div>
		<!--尾部-->
		<!--底部代码-->
	    <div>
			<div class="wrap_bottom"><img src="../../image/bottom.png"></div>
		</div>
	</body>
	<script src="../../assets/jquery-3.1.1.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../js/url.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		
			/*弹框*/
			var left = ((document.body.clientWidth) - 435)/2;
			function pop(){
				$('.pop').css('left',left)
				$('.pop1').css('display','block');
				$('.pop').css('display','block');
			};
			function pop1(){
				$('.pop1').css('display','none');
				$('.pop').css('display','none');
			};
			$('.pop-img').click(function(){
				pop1();
			});
			$('.pop-y').click(function(){
				pop1();
			});
			/*弹框结束*/
			$('.main-add').on('click',function(){
	         	 window.location.href='../mine/mine_mian.html';
	         	 localStorage.wyshopwheretopage=11;
        	 });
        	 $('.main-per2').click(function(){
        	 	 window.location.href='../mine/mine_mian.html';
	         	 localStorage.wyshopwheretopage=11;
        	 })
//			总金额
			function newPrice(){
				var moneyAll = $('.moneyAll').text().replace('￥','');
				var usecoun = $('.usecoun').text().replace('￥','');
				var useyun = $('.useyun').text().replace('￥','');
				var newPrice = parseFloat(moneyAll) - parseFloat(usecoun) + parseFloat(useyun);
				$('.newPrice').text('￥' + newPrice.toFixed(2));
			};
			//提交订单
			
			$('.main-dd').click(function(){
				var sel1 = $('.main-per').children().hasClass('activityLog');
				if(sel1 == true){
					$('.pop-text').text('您还没有收货地址，前往新增地址');
					pop();
				}else if($('input[name="select"]').val()==''){
					$('.pop-text').text('请选择配送方式');
					pop();
				} else{
					console.log('提交订单成功');
					creatOrder();
				}
			});
//			配送方式
			var sel=true;
			$('input[name="select"]').click(function(){
				if(sel){
					$('.main-ps1 input').css('display','block');
					sel=false;
				}else{
					$('.main-ps1 input').css('display','none');
					sel=true;
				}
			});
			function useyun(){
				$('.main-ps1 input').click(function(){
					localStorage.yunid = $(this).attr('id');//传选中是物流id
					$('input[name="select"]').val($(this).val());
					$('.main-ps1 input').css('display','none');
					sel=true;
					$('.useyun').text('￥'+$('input[name="select"]').val().match(/\d+/g));
					newPrice();//总金额
				});
			};
			
			//所有input、textarea点击事件
			$('input').click(function(){
				$(this).css('border','1px solid #FFC100');
			});
			$('textarea').click(function(){
				$(this).css('border','1px solid #FFC100');
			});
			/*使用优惠券*/
			$('input[name="coupon"]').click( function() {
				$('.coupon').css('background-color','#FFC100');
			});
			$('.coupon').click(function(){
				if($('input[name="coupon"]').val()==''){
					$('.pop-text').text('优惠卷号码不能为空');
					pop();
				}else{
					usecoun();
				}
			});
//			判断border
			function coupon(){
				if($('input[name="coupon"]').val()==''){
					$('.coupon').css('background-color','#ccc');
					$('input[name="coupon"]').css('border','1px solid #ccc');
				};
				if($('textarea').val()==''){
					$('textarea').css('border','1px solid #ccc');
				};
				if($('input[name="select"]').val()==''){
					$('input[name="select"]').css('border','1px solid #ccc');
				};
			};
//			收货地址的获取接口
			showaddress();
			function showaddress() {
				$.ajax({
						url: ur + '/mobilemember/frontmember/getDefauReceiAddr',
						type: 'POST',
						headers: {
							"X-Token": localStorage.wyshoptoken
						},
						dataType: 'json',
						data: '',
						success:function(data){
//							console.log(JSON.stringify(data));
							if(data.result.meta.code == 200) {
								$('.name').text(data.result.data.reciver);
								$('.tel').text(data.result.data.phone);
								$('.area').text(data.result.data.area);
								$('.details').text(data.result.data.detail);
							} else if(data.result.meta.code == 201) {
								$('.main-per2').css('display','block');
								$('.main-per2').addClass('activityLog');
								$('.main-per1').css('display','none');
								$('.pop-text').text('您还没有默认地址，前往设置默认地址');
								pop();
								$('.pop-y').click(function(){
									pop1();
									window.location.href='../mine/mine_mian.html';
									localStorage.wyshopwheretopage=11;
								});
								$('.main-error').css('display','block');
								$('.main-error').text('您还没有默认地址，前往设置默认地址');
							} else if(data.result.meta.code == 202) {
								$('.main-per2').css('display','block');
								$('.main-per2').addClass('activityLog');
								$('.main-per1').css('display','none');
								$('.pop-text').text('您还没有收货地址，前往新增地址');
								pop();
								$('.pop-y').click(function(){
									pop1();
									window.location.href='../mine/mine_mian.html';
									localStorage.wyshopwheretopage=11;
								});
	         					localStorage.wyshopwheretopage=11;
							}
						},
						error:function(error){
//							console.log(error+'收货地址')
						}
				});
			};
			/*获取确认购买商品展示*/
			showsure();
			function showsure() {
				$.ajax({
						url: ur + '/mobilemember/shoppingcart/confirmationOfOrder',
						type: 'POST',
						headers: {
							"X-Token": localStorage.wyshoptoken
						},
						dataType: 'json',
						data: {
							"cskuIds": localStorage.skid,//商品库存id的集合
							"ccounts": localStorage.how,//数量集合
							"cprices": localStorage.pricejh,//单价集合
							"cproductIds": localStorage.proid,//商品id的集合
						},
						success:function(data){
							console.log(JSON.stringify(data)+">><<<获取确认购买商品展示");
							var moneyAll=0;//总金额
							if(data.result.meta.code == 200) {
								var scount = '';
								var sname = '';
								var sdan = '';
								var skid7 = '';
								
								var jdcount = '';
								var jdname = '';
								var jdprice = '';
								var jdpskuids = '';
							
								for(var i = 0; i < data.result.data.length; i++) {
									var b =  data.result.data[i].imgsrc;
									var m = b.indexOf(',');
									if(m != '-1') {
										var mainImg = b.substring(0, m);
									} else {
										mainImg = b;
									}
									var money=data.result.data[i].price*data.result.data[i].number;
									sname = sname + data.result.data[i].shortName + ',';
									
									var div=$('<div class="main-goods">'+
												'<div style="background-image: url('+picur + mainImg+');"></div>'+
												'<div class="main-goods1">'+'<div></div>'+
													'<div>'+
														'<span>'+
															'<span>'+data.result.data[i].shortName+'</span>'+
															'<span></span>'+
														'</span>'+
														'<span>'+data.result.data[i].size+'</span>'+
														'<span>￥'+data.result.data[i].price+'</span>'+
														'<span>'+ data.result.data[i].number+'</span>'+
														'<span>￥'+money+'</span>'+
													'</div>'+
												'</div>'+
											'</div>');
									$('.main-good').append(div);
									moneyAll+=money;
										if(data.result.data[i].jdProductId!=null && data.result.data[i].jdProductId!=''){
//		                                	alert(data.result.data[i].jdProductId);
		                                	jdcount = jdcount + data.result.data[i].number + ',';
		                                	jdname = jdname + data.result.data[i].shortName + ',';
		                                	jdprice = jdprice + data.result.data[i].price + ',';
		                                	jdpskuids = jdpskuids + data.result.data[i].skuId + ',';
		                                }else{
		                                	scount = scount + data.result.data[i].number + ',';
										    sname = sname + data.result.data[i].shortName + ',';
										    sdan = sdan + data.result.data[i].price + ',';
										    skid7 = skid7 + data.result.data[i].skuId + ',';
		                                }
								}
								localStorage.allprice=moneyAll;//总商品金额缓存优惠卷使用
								$('.moneyAll').text("￥"+moneyAll);//总金额
								newPrice();
								/*获取运费接口*/
								getyun();
								//缓存创建订单
								
							}
								localStorage.jdcount = jdcount.substring(0, jdcount.length - 1);
								localStorage.jdname = jdname.substring(0, jdname.length - 1);
								localStorage.jdprice = jdprice.substring(0, jdprice.length - 1);
								localStorage.jdpskuids = jdpskuids.substring(0, jdpskuids.length - 1);
								console.log(localStorage.jdcount);
								console.log(localStorage.jdname);
								console.log(localStorage.jdprice);
								console.log(localStorage.jdpskuids);
//								localStorage.nature = startall.substring(0, startall.length - 1);
console.log(skid7)
								localStorage.scount = scount.substring(0, scount.length - 1);						
								localStorage.sname = sname.substring(0, sname.length - 1);
								localStorage.sdan = sdan.substring(0, sdan.length - 1);
								localStorage.skid7 = skid7.substring(0, skid7.length - 1);
		                        console.log(localStorage.scount);
								console.log(localStorage.sname);
								console.log(localStorage.sdan);
								console.log(localStorage.skid7);
						},
						error:function(){
							
						}
					})
			}
			/*获取运费接口*/
			
			function getyun() {
				if(localStorage.proid.indexOf(",") > 0) {
					proidan = localStorage.proid.substring(0, localStorage.proid.indexOf(","));
				} else {
					proidan = localStorage.proid;
				}
				$.ajax({
						url: ur + '/mobile/freighttemplate/getfreightprice',
						type: 'POST',
						headers: {
							"X-Token": localStorage.wyshoptoken
						},
						dataType: 'json',
						data: {
							"area": $('.area').text(),
							"conunts": localStorage.how,
							"productIds": localStorage.proid,
						},
						success:function(data){
//							console.log(JSON.stringify(data));
							if(data.result.meta.code == 200) {
//								console.log(data.result.data.length);
								for(var p = 0; p < data.result.data.length; p++) {
									var div = $('<input type="text" readonly="readonly" name="" id="'+data.result.data[p].expressId+'" value="'+data.result.data[p].expressName+'&nbsp;&nbsp;&nbsp;'+data.result.data[p].freightPrice+'元'+'" placeholder="" />');
									$('.main-ps1').append(div)
								}
								useyun();//操作事件
							} else if(data.result.meta.code == 202 || data.result.meta.code == 201) {
								$('.main-ps>input').val('此地区无配送方式');
							}
						},
						error:function(error){
							console.log('运费系统出错')
						}
					})
			}
//			优惠卷接口
			function usecoun() {
				$.ajax({
						url: ur + '/mobilemember/shoppingcart/getByCoupons',
						type: 'POST',
						headers: {
							"X-Token": localStorage.wyshoptoken
						},
						dataType: 'json',
						data: {
							"couponsNo": $('input[name="coupon"]').val(),
							"totalPrice": localStorage.allprice
						},
						success:function(data){
							if(data.result.meta.code == 202) {
								$('.pop-text').text('优惠券已过期或者已被使用');
								pop();
								$('input[name="coupon"]').val('');
							} else if(data.result.meta.code == 201) {
								$('.pop-text').text('优惠券不存在');
								pop();
								$('input[name="coupon"]').val('');
							} else if(data.result.meta.code == 200) {
								console.log('优惠券使用成功');
								$('.coupon').unbind('click');
								$('.coupon').text('已使用');
								$('.usecoun').text('');
								var newPrice1 = data.result.data.newPrice.toFixed(2).replace('-','');
								var newPrice2 = $('.moneyAll').text().replace('￥','');
								var newPrice = newPrice2 - newPrice1;
								console.log(newPrice.toFixed(2))
								console.log(newPrice1+'>>>'+newPrice2+'>>>'+newPrice)
								$('.usecoun').text('￥-'+newPrice.toFixed(2));
								newPrice();
								localStorage.youhuiquan = $('input[name="coupon"]').val();//优惠卷号码
							}
						},
						error:function(error){
							console.log('系统错误');
						}
					})
			}
				console.log(localStorage.how+'数量集合');
				console.log(localStorage.sname+'名字集合');
				console.log(localStorage.pricejh+'价格集合');
				console.log(localStorage.skid+'库存集合');
				console.log(localStorage.wyshoptoken);
			/*创建订单*/
			function creatOrder() {
				console.log($('.area').text()+'》》地址');
				console.log($('.details').text()+'》》详细地址');
				console.log(localStorage.yunid+"快递的选择");
				console.log($('.main-ps input').val().match(/\d+/g)+'》》快递费');
				console.log($('textarea').val()+'》》留言');
				console.log($('.newPrice').text()+'总价格');
				console.log($('.tel').text()+'》》电话号码');
				console.log($('.name').text()+'》》名字');
				console.log(localStorage.how+'数量集合');
				console.log(localStorage.sname+'名字集合');
				console.log(localStorage.pricejh+'价格集合');
				console.log(localStorage.skid7+'库存集合');
				console.log(localStorage.wyshoptoken);
				newPrice();
				
				var addressArea = $('.area').text().split(" ");
				console.log(addressArea);
//				alert(addressArea);
				if(addressArea.length==3){
					var sheng7 = addressArea[0];
					var shi7 = addressArea[1];
					var qu7 = addressArea[2];
					var jie7 = '';
					
				}else if(addressArea.length==4){
					var sheng7 = addressArea[0];
					var shi7 = addressArea[1];
					var qu7 = addressArea[2];
					var jie7 = addressArea[3];
				}
				
//				alert(localStorage.jdcount);
//				alert(localStorage.jdname);
//				alert(localStorage.jdprice);
//				alert(localStorage.jdpskuids);
				$.ajax({
						url: ur + '/mobilemember/frontOrder/createOrder',
						type: 'POST',
						headers: {
							"X-Token": localStorage.wyshoptoken
						},
						dataType: 'json',
						data: {
							"orderVO.city":shi7,
							"orderVO.province":sheng7,
							"orderVO.country":qu7,
							"orderVO.town":jie7,
							"jdPcounts":localStorage.jdcount,
							"jdPnames":localStorage.jdname,
							"jdPprice":localStorage.jdprice,
							"jdPskuids":localStorage.jdpskuids,
							"enterProductIds":localStorage.enterP,//企业商品编号
							"orderVO.area": $('.area').text(),//	收货区域
							"orderVO.detail": $('.details').text(),//	收货详细地址
							"orderVO.expressId": localStorage.yunid,//订单物流id
							"orderVO.freight": $('.main-ps1 input').val().match(/\d+/g),//订单物流费
							"orderVO.leaveWord": $('textarea').val(),//订单留言
							"orderVO.orderPay": $('.newPrice').text().replace('￥',''),//订单总金额
							"orderVO.phone": $('.tel').text(),//收货手机号码
							"orderVO.reciver": $('.name').text(),//收货人
							"orderVO.remark": '',//疑问    备注，将商品的属性传过来
							"pcounts": localStorage.scount,//	商品数量的集合000
							"pnames": localStorage.sname,//	商品名称的集合111
							"pprice": localStorage.sdan,//商品单价的集合000
//							"pskuids":parseInt(localStorage.skid),
							"pskuids":  localStorage.skid7,//	商品skuid的集合000
							"type": 2,
							"comSkuIds": localStorage.zhproid//组合商品的库存编号
						},
						success:function(data){
							console.log(JSON.stringify(data));
							if(data.result.meta.code == 200) {
								console.log('创建订单成功');
								localStorage.sureorder = data.result.data.orderNum;
								localStorage.sureorderid = data.result.data.orderId;//订单ID
								delectgoods();
								
								
							}else if(data.result.meta.code == 202){
								alert('系统繁忙，请稍后再试');
							}
						},
						error: function(XMLHttpRequest, textStatus, errorThrown) {
						 console.log(XMLHttpRequest);
						 console.log(errorThrown);
						 console.log(textStatus);
						   },
//						error:function(error){
//							console.log(JSON.stringify(error))
//							console.log('系统错误创建订单')
//						}
					})
			}
			
		
		/*创建订单删除购物车和优惠券报废*/
			function delectgoods(){

				$.ajax({
					url: ur + '/mobilemember/shoppingcart/deleteshoppingcartByOrder',
					type: 'POST',
					headers: {
						"X-Token": localStorage.wyshoptoken
					},
					dataType: 'json',
					data: {
						"couponsno": localStorage.youhuiquan,//优惠卷号码
						"ordercouId": localStorage.sureorderid//订单ID
					},
					success: function(data){
//						console.log(JSON.stringify(data));
                        localStorage.youhuiquan = '';
//                      window.location.replace("pay.html");
					},
					error: function(data) {
						console.log('错误优惠券报废');
					}
				})
			}
	</script>
</html>